package com.infore.dataReceiving.netty.client;
import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.infore.dataReceiving.util.Air212InstructionsHandleUtil;

import io.netty.channel.ChannelHandlerContext;  
import io.netty.channel.SimpleChannelInboundHandler;  
import io.netty.channel.ChannelHandler.Sharable;

@Component("clientHandler")
@Sharable
public class ClientHandler extends SimpleChannelInboundHandler<String> {  
  
    private static final Logger log = LoggerFactory.getLogger(ClientHandler.class);  
      
    @Override  
    public void channelRead0(ChannelHandlerContext ctx, String instruction)  
            throws Exception {  
    	instruction = instruction+"\r\n";
    	System.out.println(instruction);
    }  
    
    @Override  
    public void channelActive(ChannelHandlerContext ctx) throws Exception {  
        super.channelActive(ctx);
    }  
      
  
    @Override  
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
    	Air212InstructionsHandleUtil.remoteMap.put("remoteFlag", "1");
        cause.printStackTrace();  
        ctx.close();  
    }  
  
    @Override  
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    	Air212InstructionsHandleUtil.remoteMap.put("remoteFlag", "1");
        super.channelInactive(ctx);  
        ctx.close();
    }  
}
